package com.xmad.ksadutils.AdSDK.AdMold.Kwai;


import android.app.Activity;
import android.os.Handler;
import android.util.Log;

import androidx.annotation.Nullable;
import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsFullScreenVideoAd;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.SdkConfig;
import com.xmad.ksadutils.AdSDK.AdBase.AdBase;
import com.xmad.ksadutils.AdSDK.Utlis.AdComplete_;
import com.xmad.ksadutils.AdSDK.Utlis.Click;
import com.xmad.ksadutils.AdSDK.Utlis.Tool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class KsFullScreenAd extends AdBase {
    private volatile static KsFullScreenAd mInstance;
    KsFullScreenVideoAd mFullScreenVideoAd;
    static Map<Long, AdComplete_> mMap= new HashMap<Long,AdComplete_>();
    /**
     * 单例模式
     *
     * @return
     */
    public static KsFullScreenAd getInstance() {
        if (mInstance == null) {
            synchronized (KsFullScreenAd.class) {
                if (mInstance == null) {
                    mInstance = new KsFullScreenAd();
                }
            }
        }
        return mInstance;
    }


    @Override
    protected void load() {
        if (getKSid()==null||getKSid().length==0){
            Log.e(TAG,"先设置梯度ID");
            return;
        }
        //新建
        NewMap();
        //获取广告对象List列表
        GetAdList();
    }


    //新建
    public void NewMap(){
        Log.e(TAG,mMap.toString());
        if (mMap.size()==0){
            for (Long Id:getKSid()){
                mMap.put(Id,null);
            }
        }
    }

    //获取广告对象List列表
    public void GetAdList(){
        Log.e(TAG,reveal+"---------------");
        if (reveal){
            start();
        }
        for(Long key:mMap.keySet()){
            if (mMap.get(key)==null||mMap.get(key).getAd()==null){
                getAds(key);
            }
        }


    }





    private void   getAds(Long id){
        Log.e(TAG,"激励视频————加载————"+id);
        KsScene.Builder builder = new KsScene.Builder(id).screenOrientation(SdkConfig.SCREEN_ORIENTATION_UNKNOWN);
        Map<String, String> rewardCallbackExtraData = new HashMap<>();
        rewardCallbackExtraData.put("thirdUserId", "test-uerid-jia");
        rewardCallbackExtraData.put("extraData", "testExtraData");
        builder.rewardCallbackExtraData(rewardCallbackExtraData);
        KsScene scene = builder.build(); // 此为测试posId，请联系快手平台申请正式posId
        KsAdSDK.getLoadManager().loadFullScreenVideoAd(scene,
                new KsLoadManager.FullScreenVideoAdListener() {
                    @Override
                    public void onError(int code, String msg) {
                        Log.e(TAG,"激励视频onError"+code+msg+"-----"+id);
                        mMap.put(id,new AdComplete_(true,null));
                        if (listener!=null){
                            listener.onError(code+msg);
                        }
                    }
                    @Override
                    public void onRequestResult(int adNumber) { }
                    @Override
                    public void onFullScreenVideoAdLoad(@Nullable List<KsFullScreenVideoAd> adList) {
                        Log.e(TAG,"激励视频成功"+id);
                        if (adList != null && adList.size() > 0) {
                            mFullScreenVideoAd = adList.get(0);
//                            showFullScreenVideoAd(activity,null);
                            mMap.put(id,new AdComplete_(true,mFullScreenVideoAd));
                            // 广告展示
//                            if (reveal){
//                                start();
//                            }
                        }
                    }
                });
    }



    // 广告展示
    @Override
    public void start(){
        if (Is_Show()){
            Log.e(TAG,"Is_Show------");
            for (Long id:getKSid()){
                if (mMap.get(id).getAd()!=null){
                    showFullScreenVideoAd(activity,mMap.get(id).getAd(),id);
                    Log.e(TAG,"id---------"+id);
                    mMap.put(id,null);
                    break;
                }
            }
        }else {
            Log.e(TAG,"需要继续");
        }
    }

    //查询是否进行广告展示
    private boolean Is_Show(){
        boolean Complete = false;
        for (Long key : mMap.keySet()) {
            if (mMap.get(key)!=null){
                if (mMap.get(key).isComplete()){
                    Complete=mMap.get(key).isComplete();
                }else {
                    return false;
                }
            }else {
                return false;
            }
        }
        return Complete;
    }


    private void showFullScreenVideoAd(Activity activity,final KsFullScreenVideoAd mFullScreenVideoAd,Long id) {
        if (mFullScreenVideoAd != null && mFullScreenVideoAd.isAdEnable()) {
            mFullScreenVideoAd
                    .setFullScreenVideoAdInteractionListener(new KsFullScreenVideoAd.FullScreenVideoAdInteractionListener() {
                        @Override
                        public void onAdClicked() {
                            //ToastUtil.showToast(mContext, "全屏视频广告点击");
                            Log.e(TAG,"激励视频广告点击");
                            Click.Jump=true;
                        }

                        @Override
                        public void onPageDismiss() {
                            //ToastUtil.showToast(mContext, "全屏视频广告关闭");
                            Log.e(TAG,"激励视频广告关闭");
                            if (listener!=null){
                                listener.onPageDismiss();
                            }
                        }

                        @Override
                        public void onVideoPlayError(int code, int extra) {
                            //ToastUtil.showToast(mContext, "全屏视频广告播放出错");
                        }

                        @Override
                        public void onVideoPlayEnd() {
                            // ToastUtil.showToast(mContext, "全屏视频广告播放完成");
                        }

                        @Override
                        public void onVideoPlayStart() {
                            // ToastUtil.showToast(mContext, "全屏视频广告播放开始");
                            Log.e(TAG,"激励视频广告播放开始");
                            if (listener!=null){
                                listener.onAdShow();

                            }
                        }

                        @Override
                        public void onSkippedVideo() {
                            //ToastUtil.showToast(mContext, "全屏视频广告播放跳过");
                            if (listener!=null){
                                listener.onJump(200);
                            }

                        }
                    });
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    mFullScreenVideoAd.showFullScreenVideoAd(activity, null);
                }
            },3000);
        } else {
            Log.e(TAG,"暂无可用全屏视频广告，请等待缓存加载或者重新刷新");
        }
    }



}
